﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

namespace cwdz
{
    public partial class bodyDetail : System.Web.UI.Page
    {
        Decimal _17TAXTOTAL = 0m;
        Decimal _13TAXTOTAL = 0m;
        Decimal _WJTOTAL = 0m;
        Decimal _HJTOTAL = 0m;
            
        protected void Page_Load(object sender, EventArgs e)
        {
            String BillNo = Request["billNo"];
            BillNoLabel.Text = BillNo;
            
            //OrgNameLabel.Text = Request["OrgName"];
            //SupCodeLabel.Text = Request["SupCode"];
            //SupNameLabel.Text = Request["SupName"];
            //HtCodeLabel.Text = Request["HtCode"];
            //HtNameLabel.Text = Request["HtName"];

            GridView_bodyDetail.DataSource = bodyDetailDataSet(BillNo);
            GridView_bodyDetail.DataBind();
        }
        protected DataSet bodyDetailDataSet(String BillNo)
        {
            //连接字符串
            //需要优化成动态的
            String connectionString = "Provider=msdaora;Data Source=zbcmp;Persist Security Info=True;Password=Cmp_Data;User ID=hscmp";
            
            //新建数据集
            DataSet buildDataSet = new DataSet();

            //新建连接            
            OleDbConnection buildConnection = new OleDbConnection(connectionString);

            //打开链接
            buildConnection.Open();
            string selectCommand = buildSQLCommandDetail(BillNo);
            OleDbDataAdapter buildAdapter = new OleDbDataAdapter(selectCommand, connectionString);

            //填充DataSet
            buildAdapter.Fill(buildDataSet);

            //注销适配器
            buildAdapter.Dispose();
            //关闭连接
            buildConnection.Close();

            //返回数据集
            return buildDataSet;
        }
        protected String buildSQLCommandDetail(String BillNo)
        {
            
            return "SELECT YWORGNAME AS 业务组织名称,YWBILLNO AS 业务单据号,REMARK AS 业务类型,CASE JTAXRATE WHEN 17 THEN SUM(JTaxTotal) ELSE 0 END AS \"17%税额\", CASE JTAXRATE WHEN 13 THEN SUM(JTaxTotal) ELSE 0 END AS \"13%税额\",SUM(WJTOTAL) AS \"未税金额\",SUM(HJTOTAL) AS \"含税金额\" FROM TACPJSJXBODY WHERE BILLNO = '" +BillNo+ "' GROUP BY YWORGNAME,BILLNO,YWBILLNO,REMARK,JTAXRATE ORDER BY YWBILLNO";
        }
        protected void GridView_bodyDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView totalRows = (System.Data.DataRowView)e.Row.DataItem;
                for (int i = 0; i < GridView_bodyDetail.HeaderRow.Cells.Count; i++)
                {
                    if (GridView_bodyDetail.HeaderRow.Cells[i].Text.Equals("17%税额"))
                    {
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                        _17TAXTOTAL += Convert.ToDecimal(totalRows[i].ToString());
                        //_17TAXTOTAL = 25;
                    }
                    else if (GridView_bodyDetail.HeaderRow.Cells[i].Text.Equals("13%税额"))
                    {
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                        _13TAXTOTAL += Convert.ToDecimal(totalRows[i].ToString());
                    }
                    else if (GridView_bodyDetail.HeaderRow.Cells[i].Text.Equals("未税金额"))
                    {
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                        _WJTOTAL += Convert.ToDecimal(totalRows[i].ToString());
                    }
                    else if (GridView_bodyDetail.HeaderRow.Cells[i].Text.Equals("含税金额"))
                    {
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                        _HJTOTAL += Convert.ToDecimal(totalRows[i].ToString());
                    }
                }
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
                e.Row.Cells[0].Text = "合计：";

                for (int i = 0; i < GridView_bodyDetail.HeaderRow.Cells.Count; i++)
                {
                    if (GridView_bodyDetail.HeaderRow.Cells[i].Text.Equals("17%税额"))
                    {
                        e.Row.Cells[i].Text = _17TAXTOTAL.ToString();
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                    }
                    else if (GridView_bodyDetail.HeaderRow.Cells[i].Text.Equals("13%税额"))
                    {
                        e.Row.Cells[i].Text = _13TAXTOTAL.ToString();
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                    }
                    else if (GridView_bodyDetail.HeaderRow.Cells[i].Text.Equals("未税金额"))
                    {
                        e.Row.Cells[i].Text = _WJTOTAL.ToString();
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                    }
                    else if (GridView_bodyDetail.HeaderRow.Cells[i].Text.Equals("含税金额"))
                    {
                        e.Row.Cells[i].Text = _HJTOTAL.ToString();
                        e.Row.Cells[i].HorizontalAlign = HorizontalAlign.Right;
                    }
                }
            }
        }
    }
}